/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MemberForm.java
 *
 * Created on 23-03-2011, 22:31:56
 */
package tiebreak.UI;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JTextField;
import tiebreak.BE.*;
import tiebreak.BL.EasyException;
import tiebreak.BL.MemberWorker;

/**
 *
 * @author AngelwingedDevil
 */
public class MemberForm extends javax.swing.JPanel
{
    private Member member;

    /** Creates new form MemberForm */
    public MemberForm()
    {
        initMyComponents();
    }

    public MemberForm(int cprseq) throws EasyException
    {
        MemberWorker mw = new MemberWorker();
        Member m;
        if(cprseq>4)
            m=mw.getMemberWithCpr(cprseq);
        else
            m=mw.getMemberWithMemberID(cprseq);
        initValues(m);
    }

    public MemberForm(Member m)
    {
        initMyComponents();
        initValues(m);
    }
    
    private void initValues(Member m)
    {
        member = m;
        txtFirstName.setText(m.getFirstName());
        txtLastName.setText(m.getLastName());
        txtAddress.setText(m.getAddress());
        dropType.setSelectedIndex(m.getTypeID() - 1);
        txtCpr.setText(m.getCpr() + "");
        txtPhone.setText(m.getPhone() + "");
        txtZip.setText(m.getZipcode() + "");
        txtMail.setText(m.getEmail());
        chbElite.setSelected(m.getIsElite());
        chbSchool.setSelected(m.getUnderSchoolTraining());
        chbCoach.setSelected(m.getIsCoach());
    }

    public Member getMember()
    {
        return member;
    }

    private void initMyComponents()
    {
        initComponents();
        setupMembershipType();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblFirstName = new javax.swing.JLabel();
        lblLastName = new javax.swing.JLabel();
        lblAddress = new javax.swing.JLabel();
        lblZip = new javax.swing.JLabel();
        lblCpr = new javax.swing.JLabel();
        lblPhone = new javax.swing.JLabel();
        lblEmail = new javax.swing.JLabel();
        lblType = new javax.swing.JLabel();
        lblDescription = new javax.swing.JLabel();
        txtFirstName = new javax.swing.JTextField();
        txtLastName = new javax.swing.JTextField();
        txtAddress = new javax.swing.JTextField();
        txtCpr = new javax.swing.JTextField();
        txtZip = new javax.swing.JTextField();
        txtMail = new javax.swing.JTextField();
        txtPhone = new javax.swing.JTextField();
        dropType = new javax.swing.JComboBox();
        chbElite = new javax.swing.JCheckBox();
        chbSchool = new javax.swing.JCheckBox();
        chbCoach = new javax.swing.JCheckBox();

        lblFirstName.setText("*First Name");

        lblLastName.setText("*Last Name");

        lblAddress.setText("*Address:");

        lblZip.setText("*Zip");

        lblCpr.setText("*cpr");

        lblPhone.setText("*phone");

        lblEmail.setText("*email");

        lblType.setText("*Type");

        lblDescription.setText("* = Required fields");

        chbElite.setText("*Elite");

        chbSchool.setText("*Tennis School");

        chbCoach.setText("*Coach");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblCpr)
                    .addComponent(lblZip)
                    .addComponent(lblPhone)
                    .addComponent(lblEmail)
                    .addComponent(lblAddress)
                    .addComponent(lblType)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(lblLastName)
                        .addComponent(lblFirstName)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(chbCoach)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(chbSchool)
                            .addComponent(txtMail, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
                            .addComponent(txtZip, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
                            .addComponent(txtCpr, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
                            .addComponent(txtAddress, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
                            .addComponent(txtFirstName, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
                            .addComponent(txtLastName, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
                            .addComponent(txtPhone, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
                            .addComponent(dropType, 0, 118, Short.MAX_VALUE)
                            .addComponent(chbElite, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE))
                        .addContainerGap(60, Short.MAX_VALUE))))
            .addGroup(layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addComponent(lblDescription)
                .addContainerGap(124, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblFirstName)
                    .addComponent(txtFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblLastName)
                    .addComponent(txtLastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblAddress)
                    .addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblZip)
                    .addComponent(txtZip, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCpr)
                    .addComponent(txtCpr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPhone)
                    .addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblEmail)
                    .addComponent(txtMail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblType)
                    .addComponent(dropType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chbElite)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chbSchool)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chbCoach)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblDescription)
                .addContainerGap(19, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox chbCoach;
    private javax.swing.JCheckBox chbElite;
    private javax.swing.JCheckBox chbSchool;
    private javax.swing.JComboBox dropType;
    private javax.swing.JLabel lblAddress;
    private javax.swing.JLabel lblCpr;
    private javax.swing.JLabel lblDescription;
    private javax.swing.JLabel lblEmail;
    private javax.swing.JLabel lblFirstName;
    private javax.swing.JLabel lblLastName;
    private javax.swing.JLabel lblPhone;
    private javax.swing.JLabel lblType;
    private javax.swing.JLabel lblZip;
    private javax.swing.JTextField txtAddress;
    private javax.swing.JTextField txtCpr;
    private javax.swing.JTextField txtFirstName;
    private javax.swing.JTextField txtLastName;
    private javax.swing.JTextField txtMail;
    private javax.swing.JTextField txtPhone;
    private javax.swing.JTextField txtZip;
    // End of variables declaration//GEN-END:variables

    public String getFirstName()
    {
        return txtFirstName.getText();
    }

    public String getLastName()
    {
        return txtLastName.getText();
    }

    public String getAddress()
    {
        return txtAddress.getText();
    }

    public int getCpr()
    {
        try
        {
            return Integer.parseInt(txtCpr.getText());
        }
        catch (Exception e)
        {
            return 0;
        }

    }

    public int getZip()
    {
        try
        {
            return Integer.parseInt(txtZip.getText());
        }
        catch (Exception e)
        {
            return 0;
        }
    }

    public int getPhone()
    {
        try
        {
            return Integer.parseInt(txtPhone.getText());
        }
        catch (Exception e)
        {
            return 0;
        }
    }

    public String getMail()
    {
        return txtMail.getText();
    }

    private void setupMembershipType()
    {
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        model.addElement(new MembershipType(1, "Junior", 450));
        model.addElement(new MembershipType(2, "Senior", 900));
        model.addElement(new MembershipType(2, "Retiree", 500));
        dropType.setModel(model);
    }

    public boolean isFirstNameValid()
    {
        return !txtFirstName.getText().equals("");
    }

    public boolean isLastNameValid()
    {
        return !txtLastName.getText().equals("");
    }

    public boolean isAddressValid()
    {
        return !txtAddress.getText().equals("");
    }

    public boolean isCprValid()
    {
        try
        {
            Integer.parseInt(txtCpr.getText());
            return !txtCpr.getText().equals("");
        }
        catch (Exception e)
        {
            return false;
        }
    }

    public boolean isPhoneValid()
    {
        try
        {
            Integer.parseInt(txtPhone.getText());
            return !txtPhone.getText().equals("");
        }
        catch (Exception e)
        {
            return false;
        }
    }

    public boolean isZipValid()
    {
        try
        {
            Integer.parseInt(txtZip.getText());
            return !txtZip.getText().equals("");
        }
        catch (Exception e)
        {
            return false;
        }
    }

    public boolean tennisSchool()
    {
        return chbSchool.isSelected();
    }

    public boolean elite()
    {
        return chbElite.isSelected();
    }

    public MembershipType getMemberType()
    {
        return (MembershipType) dropType.getSelectedItem();
    }

    public boolean isTextFieldValid(JTextField field)
    {
        return !field.getText().equals("");
    }

    public boolean isNumberTextFieldValid(JTextField field)
    {
        try
        {
            Integer.parseInt(field.getText());
            return isTextFieldValid(field);
        }
        catch (Exception e)
        {
            return false;
        }
    }

    public String getInValidMessage()
    {
        String msg = "<html>Errors:<br>";
        boolean valid = true;
        if (!isTextFieldValid(txtFirstName))
        {
            msg += "There was an error with first name<br>";
            valid = false;
        }
        if (!isTextFieldValid(txtLastName))
        {
            msg += "There was an error with the last name<br>";
            valid = false;
        }
        if (!isTextFieldValid(txtAddress))
        {
            msg += "There was an error with the Address<br>";
            valid = false;
        }
        if (!isTextFieldValid(txtMail))
        {
            msg += "THere was an error with the Address<br>";
            valid = false;
        }
        if (!isNumberTextFieldValid(txtCpr))
        {
            msg += "There was an error with the Cpr<br>";
            valid = false;
        }
        if (!isNumberTextFieldValid(txtPhone))
        {
            msg += "There was an error with the PhoneNumber<br>";
            valid = false;
        }
        if (!isNumberTextFieldValid(txtZip))
        {
            msg += "there was a problem with the zip code<br>";
            valid = false;
        }
        if (valid)
        {
            msg += "There were no errors<br>";
        }
        else
        {
            msg += "Please check the form, if there are letters in fields that should only display numbers<br>Or if they are empty you should correct theese";
        }
        return msg+"</html>";
    }

    public boolean isFormValid()
    {
        if (!isTextFieldValid(txtFirstName))
        {
            return false;
        }
        if (!isTextFieldValid(txtLastName))
        {
            return false;
        }
        if (!isTextFieldValid(txtAddress))
        {
            return false;
        }
        if (!isTextFieldValid(txtMail))
        {
            return false;
        }
        if (!isNumberTextFieldValid(txtCpr))
        {
            return false;
        }
        if (!isNumberTextFieldValid(txtPhone))
        {
            return false;
        }
        if (!isNumberTextFieldValid(txtZip))
        {
           return false;
        }
        return true;
    }

    public boolean isCoach()
    {
        return chbCoach.isSelected();
    }
}
